/*
 *  TrackLearner.h
 *  powaah
 *
 *  Created by Tim Uusitalo on 4/26/11.
 *  Copyright 2011 __MyCompanyName__. All rights reserved.
 *
 */

#ifndef TRACK_LEARNER_H

#define TRACK_LEARNER_H

#include "CarControl.h"

enum TrackTypes
{
	TRACK_TYPE_NONE,
	TRACK_TYPE_LEFT,
	TRACK_TYPE_RIGHT
};

struct TrackNode
{
	float width;
	float angle;
	float dist_raced;
	TrackTypes track_type;
	float k;
	float radius;
	float length;
	float speed;
	int corner;
	TrackNode()
	: width(0), angle(0), dist_raced(0), track_type(TRACK_TYPE_NONE), k(0), radius(200), length(0), speed(0), corner(0)
	{
	}
	
};



#include "CarState.h"
#include "StdInclude.cpp"
#include "Vector2.h"

class TrackLearner
{
public:
	TrackLearner();
	~TrackLearner();
	
	void update(CarState& cs);
	
	void record(CarState& cs, float width, float* const angles, const float turn, CarControl cc);
	
	TrackNode getClosestNode(const float dist);
	
	const float getDistToNextTurn(const float current, TrackNode* const node = NULL);
	
	void save();
	bool load();
	

	
	const float calculateRadius(const float p0, const float a0);

	const float calculateRadius(const float p0, const float p1, const float a0, const float a1);
	
	const float calculateRadiusForTurn(const unsigned int start);
	
	const int getCornerNr();
	
	void evaluateCorners();
	
	const float getCornerOffset(const unsigned int i);
	
	const float getWidth() const;
private:
	std::vector<TrackNode> track;
	std::vector<int> corner_offsets;
	TrackNode closest_turn;
	unsigned int closest_nr;
	TrackNode current;
	unsigned int current_nr;
	
	int nr_corners;
	int current_corner;
	
	bool off_road_lock;
	float offtrack;
	
	Vector2 currentPosition;
	
	int last_corner;
	
	float width;
	
	
};


#endif